<?php

namespace app\api\model;

use think\Model;
use think\Db;

class SpeciModel extends Model
{

    /**
     * 颜色 尺码 列表
     * @param $level
     * @param $name
     * @param $gid
     * @param int $page
     * @param int $size
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($level, $name, $gid,$page = 10, $size=10)
    {
        if (!empty($name)) {
            $where['c3.name'] = ['like', "%" . $name . "%"];
        }
        if (!empty($level)) {
            $where['c2.level'] = $level;
        }
        if (!empty($gid)) {
            $where['c2.gid'] = $gid;
        }
        $list = Db::name('color3')->alias('c3')->join('color2 c2', 'c3.pid=c2.gid')->where($where)
            ->page($page, $size)->paginate();
        return $list;
    }

    /**
     * 分类 列表
     */
    public function getLi($data)
    {
        $where = [];
        if (!empty($data['name'])) {
            $where['name'] = ['like', "%" . $data['name'] . "%"];
        }
        if (!empty($data['pid'])) {
            $where['pid'] = $data['pid'];
        }
        $list = $list = Db::name('category')->field('id,name,pid,des')->select();
        $data = $this->gui($list);
        return $data;
    }

    /**
     * 分类新增修改  查询分类
     */
    public function getCate()
    {
        $list = Db::name('category')->field('id,name,pid')->select();
        $data = $this->gui($list);
        //print_r($data);die;
        return $data;
    }

    /**
     * 分类新增
     * @param $data
     * @return int|string
     */
    public function getCateAdd($data)
    {
        $arr = Db::name('category')->insertGetId($data);
        return $arr;
    }

    /**
     * 修改属性
     * @param $id
     * @param $data
     * @return int|string
     */
    public function getCateEdit($id, $data)
    {
        $arr = Db::name('category')->where('id', $id)->update($data);
        return $arr;
    }

    public function getCateDel($id, $attr)
    {
        $list = Db::name('goods')->where($attr, $id)->select();
        if (!empty($list)) {
            return 2;
        } else {
            $arr = Db::name('category')->where('id', $id)->delete();
            return $arr;
        }
    }


    /**
     * 递归
     * @param $data
     * @param $pId
     * @return array
     */
    function gui($data, $pId = 0)
    {
        $tree = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pId) {
                $v['son'] = $this->gui($data, $v['id']);
                $tree[] = $v;
                unset($data[$k]);
            }
        }
        return $tree;
    }

    /**
     * 属性列表
     */
    public function getAtt($pid, $name)
    {
        $where = [];
        if (!empty($name)) {
            $where['name'] = ['like', "%" . $name . "%"];
        }
        if (!empty($pid)) {
            $where['pid'] = $pid;
        }
        $list = Db::name('color1')->where($where)->select();
        return $list;
    }

    /**
     * 属性新增
     * @param $data
     * @return int|string
     */
    public function getAdd($data)
    {
        $arr = Db::name('color1')->insertGetId($data);
        return $arr;
    }

    /**
     * 颜色 尺寸新增
     * @param $data
     * @return int|string
     */
    public function colorAdd($data)
    {
        $arr = Db::name('color3')->insertGetId($data);
        return $arr;
    }

    /**
     * 颜色 尺寸修改
     * @param $id
     * @param $data
     * @return int|string
     */
    public function getEdit3($id, $data)
    {
        $arr = Db::name('color3')->where('id', $id)->update($data);
        return $arr;
    }

    /**
     * 查询属性 记录
     * @param $id
     * @return array
     */
    public function getInfo($id)
    {
        $arr = Db::name('color1')->find($id);
        if (!empty($arr['pid'])) {
            $arr['pid'] = Db::name('color')->where('cid', $arr['pid'])->value('cname');
        }
        if (!empty($arr['group'])) {
            $arr['group'] = Db::name('color2')->where('gid', $arr['group'])->value('gname');
        }
        return $arr;
    }

    /**
     * 查询颜色尺码分组数据
     * @param $id
     * @return array
     */
    public function getInfo2($id)
    {
        $arr = Db::name('color2')->find($id);
        return $arr;
    }

    /**
     * 修改属性
     * @param $id
     * @param $data
     * @return int|string
     */
    public function getEdit($id, $data)
    {
        $arr = Db::name('color1')->where('id', $id)->update($data);
        return $arr;
    }

    public function getGroupList($where)
    {
        $list =Db::name('color2')->where($where)->select();
        return $list;
    }

    /**
     * 添加分组
     * @param $data
     * @return int|string
     */
    public function getAddGroup($data)
    {
        $arr = Db::name('color2')->insertGetId($data);
        return $arr;
    }

    /**
     * 修改分组
     * @param $id
     * @param $data
     * @return int|string
     */
    public function getEdit2($id, $data)
    {
        $arr = Db::name('color2')->where('gid', $id)->update($data);
        return $arr;
    }

    /**
     * 属性删除
     * @param $id
     * @param $attr
     * @return int
     */
    public function getDel($id, $pid, $attr)
    {
        $list = Db::name('goods')->where($attr, $id)->select();
        if (!empty($list)) {
            return 2;
        } else {
            $arr = Db::name('color3')->where('id', $id)->delete();
            return $arr;
        }
    }

    public function getDelete($id, $pid, $attr)
    {
        $list = Db::name('goods')->where($attr, $id)->select();
        if (!empty($list)) {
            return 2;
        } else {
            $arr = Db::name('color1')->where('pid', $pid)->where('id', $id)->delete();
            return $arr;
        }
    }

    /**颜色分组删除
     * @param $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function colorDelGroup($id)
    {
        $list = Db::name('color3')->alias('c3')->join('color2 c2', 'c2.gid=c3.pid')
            ->join('goods g', 'g.color=c3.id')->where('c2.gid', $id)->select();
        $count = count($list);
        if ($count > 0) {
            return 2;
        } else {
            Db::name('color3')->where('pid', $id)->delete();
            $arr = Db::name('color2')->where('gid', $id)->delete();
            return $arr;
        }
    }

    /**尺码分组删除
     * @param $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sizeDelGroup($id)
    {
        $list = Db::name('color3')->alias('c3')
            ->join('color2 c2', 'c2.gid=c3.pid')
            ->join('goods g', 'g.size=c3.id')
            ->where('c2.gid', $id)->select();
        $count = count($list);
        if ($count > 0) {
            return 2;
        } else {
            Db::name('color3')->where('pid', $id)->delete();
            $arr = Db::name('color2')->where('gid', $id)->delete();
            return $arr;
        }
    }

    /**
     * 颜色 尺码新增 获取分组
     */
    public function getGroup($level)
    {
        $list = Db::name('color2')->where('gid', $level)->select();
        return $list;
    }
}
